

#ifndef _TSOC_GRAPH_
#define _TSOC_GRAPH_


#include "Scene.hpp"
#include "Math.hpp"

#include <vector>

#define KDTREE_X_SPAN	0
#define KDTREE_Y_SPAN	1
#define KDTREE_Z_SPAN	2

#define KDTREE_SPAN_LEFT	0
#define KDTREE_SPAN_CENTER	1
#define KDTREE_SPAN_RIGHT	2

#define KDTREE_INTERSECT_LEFT	1
#define KDTREE_INTERSECT_RIGHT	2


namespace com
{
	namespace toxiclabs
	{
		namespace tsoc
		{
			class Node
			{
				public:
					std::vector<Triangle *>triangles;
					Node * left;
					Node * right;
					
					int span_axis;
					float span_point;
					float span_left;
					float span_right;
					
					Vector sphere;
					float radius;
					
					Node(std::vector<Triangle *> * triangle_list,int depth);
					~Node();
					
					int GetTriangleSpanSide(int axis,float pos,Triangle * triangle);
					float GetTriangleCenter(int axis,Triangle * triangle);
					
					int Intersect(Vector &origin,Vector &direction);
					bool SphereTest(Vector &origin,Vector &direction);
					
			};
			
			
			class Graph
			{
				public:
					Node * root;
										
					std::vector<Triangle *> * triangle_list;
					
				
					Graph(std::vector<Triangle *> * triangle_list);
					
					void AnalyzeGraph();
					void Traverse(Vector & origin,Vector & direction,std::vector<Triangle *> * triangles);
					
					static bool RayTriangle(Triangle * tri,Vector & origin,Vector & direction,Vector * collision);
					
			};
		}
	}
}

#endif
